Skip to content

Fix(macros): @FILTER with a single non-array argument#5902

Open
chuenchen309 wants to merge 1 commit into
SQLMesh:mainfrom
chuenchen309:fix/filter-single-arg
Open

Fix(macros): @FILTER with a single non-array argument#5902
chuenchen309 wants to merge 1 commit into
SQLMesh:mainfrom
chuenchen309:fix/filter-single-arg

Conversation

@chuenchen309

Copy link
Copy Markdown

Description

@FILTER's docstring says "The first argument can be an Array or var args can be used" — the same contract @EACH and @REDUCE state. All three route their arguments through _norm_var_arg_lambda, whose single-item branch returns a bare expression rather than a list:

expressions = (
    item.expressions if isinstance(item, (exp.Array, exp.Tuple))
    else [item.this] if isinstance(item, exp.Paren)
    else item          # <- bare expression
)

@EACH (macros.py:714) and @REDUCE (:764) absorb that with ensure_collection(). @FILTER (:792) iterates it directly, so a lone argument raises:

@EACH(a, x -> x + 1)        -> SELECT a + 1
@REDUCE(a, (x, y) -> x + y) -> SELECT a
@FILTER(a, x -> x > 1)      -> MacroEvalError: 'Column' object is not iterable

An explicit array already worked for all three, so this only affects the lone-argument shape the docstring advertises.

Test Plan

Added two cases to test_macro_functions, next to the existing array-based @FILTER case so the two shapes sit together:

  • @SQL(@REDUCE(@FILTER(300, x -> x > 250), (x,y) -> x + y))300 (mirrors the existing [100, 200, 300, 400]700 case)
  • select @FILTER(a, x -> x > 1)SELECT a

Both fail on main and pass with this change. tests/core/test_macros.py is 140 passed with the fix, 138 passed / 2 failed without it. tests/core/test_macros.py tests/core/test_dialect.py together: 296 passed.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test)
  • My commits are signed off (git commit -s) per the DCO

On make fast-test: I ran ruff, ruff-format and the migrations hook (all pass) plus the test files above, but not the full suite. mypy reports only Cannot find implementation or library stub for module named "sqlmesh._version" in four unrelated files — that's the setuptools-scm generated module missing from my worktree (it's gitignored), not something this diff touches.

Disclosure: written with Claude Code, which I see this repo already uses. I verified the reproduction, the sibling comparison, and the test red/green myself.

@filter's docstring says "The first argument can be an Array or var args can
be used", the same contract @each and @reduce state. All three route their
arguments through _norm_var_arg_lambda, whose single-item branch returns a
bare expression rather than a list:

    expressions = (
        item.expressions if isinstance(item, (exp.Array, exp.Tuple))
        else [item.this] if isinstance(item, exp.Paren)
        else item          # <- bare expression
    )

@each and @reduce absorb that with ensure_collection(); @filter iterates it
directly, so a lone argument raises:

    @each(a, x -> x + 1)      -> SELECT a + 1
    @reduce(a, (x, y) -> x+y) -> SELECT a
    @filter(a, x -> x > 1)    -> MacroEvalError: 'Column' object is not iterable

Wrap the items in ensure_collection() so @filter matches its siblings. An
explicit array was already working and is unaffected.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: chuenchen309 <48723787+chuenchen309@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant